<?php

/**
 * This is the model class for table "zy_event_gift_password".
 *
 * The followings are the available columns in table 'zy_event_gift_password':
	 * @property integer $gift_password_id
	 * @property string $event_id
	 * @property integer $partner_id
	 * @property string $password
	 * @property integer $state
	 * @property string $time
	 * @property string $service_number
	 * @property string $bak2
	 * @property string $moblie
	 * @property string $address
	 * @property string $bak3
	 * @property integer $bak4
	 * @property string $bak5
	 * @property integer $is_delete
	 * @property string $crate_time
	 * @property string $crate_user
	 * @property string $update_time
	 * @property string $update_user
 */
class EventGiftPassword extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return EventGiftPassword the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_event_gift_password';
	}
	public function primaryKey()
	{
		return 'gift_password_id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('state, bak4, is_delete', 'numerical', 'integerOnly'=>true),
			array('event_id, password, create_user, update_user', 'length', 'max'=>50),
			array('time, service_number, bak2', 'length', 'max'=>100),
			array('moblie', 'length', 'max'=>14),
			array('address, bak3, bak5', 'length', 'max'=>300),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gift_password_id, event_id, partner_id, password, state, time, service_number, bak2, moblie, address, bak3, bak4, bak5, is_delete, create_time, create_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		 'event'=>array(self::BELONGS_TO, 'Event', 'event_id,event_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gift_password_id' =>Yii::t("EventGiftPassword","Gift Password ID"),
			'event_id' =>Yii::t("EventGiftPassword","Event ID"),
			'partner_id' =>Yii::t("EventGiftPassword","Partner ID"),
			'password' =>Yii::t("EventGiftPassword","Password"),
			'state' =>Yii::t("EventGiftPassword","State"),
			'time' =>Yii::t("EventGiftPassword","Time"),
			'service_number' =>Yii::t("EventGiftPassword","Service Number"),
			'bak2' =>Yii::t("EventGiftPassword","Bak2"),
			'moblie' =>Yii::t("EventGiftPassword","Moblie"),
			'address' =>Yii::t("EventGiftPassword","Address"),
			'bak3' =>Yii::t("EventGiftPassword","Bak3"),
			'bak4' =>Yii::t("EventGiftPassword","Bak4"),
			'bak5' =>Yii::t("EventGiftPassword","Bak5"),
			'is_delete' =>Yii::t("EventGiftPassword","Is Delete"),
			'create_time' =>Yii::t("EventGiftPassword","Create Time"),
			'create_user' =>Yii::t("EventGiftPassword","Create User"),
			'update_time' =>Yii::t("EventGiftPassword","Update Time"),
			'update_user' =>Yii::t("EventGiftPassword","Update User"),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('gift_password_id',$this->gift_password_id);
		$criteria->compare('event_id',$this->event_id,true);
			if(Yii::app()->user->level() != 1){
		   $criteria->compare('user_id',Yii::app()->user->id);
			$criteria->with = 'event';
			$criteria->compare('state','0');
		}
		$criteria->compare('password',$this->password,true);
		$criteria->compare('state',$this->state);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('service_number',$this->service_number,true);
		$criteria->compare('bak2',$this->bak2,true);
		$criteria->compare('moblie',$this->moblie,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('bak3',$this->bak3,true);
		$criteria->compare('bak4',$this->bak4);
		$criteria->compare('bak5',$this->bak5,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user',$this->create_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(  
           		 'defaultOrder'=>'gift_password_id DESC',  
       		 ),  
       		'pagination'=>array(  
           		 'pageSize'=>20  
       		 ),  
		));
	}
	
	
	
	//获取兑奖记录
	public function search2()
	{
		
		$criteria=new CDbCriteria;
		$criteria->compare('gift_password_id',$this->gift_password_id);
		$criteria->compare('event_id',$this->event_id);
		if(Yii::app()->user->level() != 1){
			$criteria->compare('user_id',Yii::app()->user->id);
			$criteria->compare('state',3);
			$criteria->with = 'event';
		}
		$criteria->compare('password',$this->password,true);
		$criteria->compare('moblie',$this->moblie,true);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
					'sort'=>array(  
           		 'defaultOrder'=>'gift_password_id DESC',  
       			 ),  
       			'pagination'=>array(  
           		 'pageSize'=>20  
       		 ), 
				));
	}
	
	/**
	 * 获取活动名称
	 */
	 function getEventName($num){
	 	if($num == ''){
	 		if(Yii::app()->user->level() != 1){
	 		$sql="select * from zy_event where 

			user_id='".Yii::app()->user->userParentId()."'";
	 		}else{
	 			$sql="select * from zy_event";
	 		}
		 	$rs = Yii::app()->db->createCommand($sql)->queryAll();
			return CHtml::listData($rs,'event_id','event_title');
	 	}else{
	 		$sql="select * from zy_event where user_id='".Yii::app()->user->userParentId()."' and event_code='".$num."'";
		 	$rs = Yii::app()->db->createCommand($sql)->queryAll();
		 	return $rs[0]['event_title'];
	 	}
	 }
	 
	 	/**
	 * 获取活动名称
	 */
	 function getEventNames($num){
	 		$sql="select * from zy_event where event_id='".$num."'";
		 	$rs = Yii::app()->db->createCommand($sql)->queryAll();
		 	return $rs[0]['event_title'];
	 }
	
	/**
	 * 兑奖状态
	 */
	 function getState($state){
	 	/*
		 	0. 没有分配给用户 (参与人数太少，少于奖品数

量  )
			1. 已经分配给用户，未领奖，未过期
			2. 已经分配给用户，未领奖，已过期
			3. 已经分配给客户，已领奖
		*/
	 	if($state == 0){
	 		return Yii::t

		('base','Unassigned,Unclaimed,Not Expired');
	 	}elseif($state == 1){
	 		return Yii::t('base','Winner,Unclaimed,Not Expired');
	 	}elseif($state == 2){
	 		return Yii::t('base','Winner,Unclaimed,Expired');
	 	}elseif($state == 3){
	 		return Yii::t('base','Winner,To accept the award');
	 	}elseif($state == 4){
	 		return Yii::t('base','Unassigned,Unclaimed,Expired');
	 	}
	 }
	 
	 //获取地址
	 function getAddress($addressid){
	 	
	 	$sql="select * from zy_award_address where 

		address_id='".$addressid."'";
	 	
	 	$rs = yii::app()->db->createCommand($sql)->queryAll();
	 	
	 	return $rs[0]['area'];
	 }
}